package com.chenyue.cm.api.version;

import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.domain.JdAppVersion;
import com.chenyue.cm.common.service.JdAppVersionService;
import com.chenyue.cm.common.service.JdConstantValuesService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author libingxing
 */
@RestController
@RequestMapping("/api/version")
public class VersionController {

    private final JdAppVersionService appVersionService;

    private final JdConstantValuesService constantValuesService;

    public VersionController(JdAppVersionService appVersionService, JdConstantValuesService constantValuesService) {
        this.appVersionService = appVersionService;
        this.constantValuesService = constantValuesService;
    }

    @GetMapping("/getVersion")
    @ApiOperation("获取最新版本")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "1安卓2ios", name = "deviceModel", dataType = "int", required = true),
    })
    public Success<JdAppVersion> getVersion(
            Integer deviceModel
    ) {
        final JdAppVersion newVersion = appVersionService.lambdaQuery()
                .eq(JdAppVersion::getDeviceModel, deviceModel)
                .orderByDesc(JdAppVersion::getVersionNum)
                .last(" limit 1").one();
        if (newVersion == null) {
            return Success.error("目前尚无新版本");
        }
        return Success.success(newVersion);
    }

    @GetMapping("/hideVersion")
    @ApiOperation("内容隐藏")
    public Success<String> hideVersion(
    ) {
//        1显示2隐藏
        return Success.success(constantValuesService.getById(5).getConstantValue());
    }
}